import { defineConfig, loadEnv } from "vite";
import { fileURLToPath, URL } from "node:url";
import vue from "@vitejs/plugin-vue";
import devtools from "vite-plugin-vue-devtools";
import eslint from "vite-plugin-eslint2";
import { createProxy } from "./build/proxy";

// https://vite.dev/config/
export default defineConfig(configEnv => {
  const viteEnv = loadEnv(configEnv.mode, process.cwd()) as unknown as ViteEnv;
  return {
    plugins: [
      vue(),
      devtools(),
      eslint({
        dev: true,
        build: true
      })
    ],
    base: viteEnv.VITE_BASE_URL,
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url)),
        "~": fileURLToPath(new URL("./", import.meta.url))
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          api: "modern-compiler",
          additionalData: "@use '@/style.scss' as *;"
        }
      }
    },
    clearScreen: false,
    server: {
      host: "0.0.0.0",
      port: 5371,
      open: true,
      proxy: createProxy(viteEnv)
    },
    esbuild: {
      drop: viteEnv.VITE_ESBUILD_DROP === "Y" ? ["console", "debugger"] : []
    },
    build: {
      sourcemap: viteEnv.VITE_SOURCE_MAP === "Y"
    }
  };
});
